package mybbs;
import java.sql.*;
import mybbs.DBConn;
import java.util.*;
public class VoteAct
{
  //String items;
 // int i=0;
  public void VoteAct(){}

  public String[] Setvote(String items){
  StringTokenizer fenxi=new StringTokenizer(items,"#");
  String b[]=new String[fenxi.countTokens()];
  int n=0;
  while(fenxi.hasMoreTokens()){
     b[n]=fenxi.nextToken();
     n++;
    }
 return b;
 }
 public void upvote(String voteitem,String postid,String title,String mode){

   try{
    DBConn conn=new DBConn();
    String CBB[]=this.Setvote(voteitem);
    conn.update("INSERT INTO vote(postid,iskey,keyid,item) VALUES("+postid+","+mode+",0,'"+title+"')");
    for(int u=0;u<CBB.length;u++){
      if(!CBB[u].equals("null")){
         int M=u+1;
         conn.update("INSERT INTO vote(postid,iskey,keyid,item,voter) VALUES("+postid+",0,"+M+",'"+CBB[u]+"','#')");
         }
       }conn.DBclose();
     }catch(Exception e){ System.out.print(e.toString());   }
    }
public boolean Hadvoted(String user,String postid){
       String username=user;
       String postidn=postid;
       boolean voted;
       try{
        DBConn cc=new DBConn();
        ResultSet rs=cc.query("SELECT * FROM vote WHERE postid="+postidn+" AND voter REGEXP '#"+username+"#'");
        if(rs.next())
          voted=true;
        else
          voted=false;
        rs.close();cc.DBclose();
        }catch(Exception e){
          System.out.print(e.toString());
          voted=false;
             }
       return voted;
    }
public int vott(String user,String keyid,String postid){
   String keyid3=keyid;
   String user3=user;
   String postid3=postid;
   int ii=0;
   try{
      DBConn con=new DBConn();
      con.update("UPDATE vote SET result=result+1 WHERE postid="+postid3+" AND iskey>0 AND islock=0");
      ii=con.update("UPDATE vote SET result=result+1,voter=CONCAT(voter,'"+user3+"','#') WHERE postid="+postid3+" AND iskey=0 AND keyid="+keyid3);
      con.DBclose();}
     catch(Exception e){
       System.out.print(e.toString());
     }
    return ii;
 }

}
